//
//  DetailListCellHeader.m
//  翼安保
//
//  Created by duzhi on 2024/4/24.
//

#import "DetailListCellHeader.h"

@interface DetailListCellHeader()
@property (strong, nonatomic) UILabel *indexL;
@property (strong, nonatomic) UILabel *nameLogoL;
@property (strong, nonatomic) UILabel *idlogoL;
@property (strong, nonatomic) UILabel *nameL;
@property (strong, nonatomic) UILabel *idL;
@property (strong, nonatomic) NewsBigButton *arrowButton;

@end

@implementation DetailListCellHeader

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self.contentView addSubview:self.indexL];
    [self.contentView addSubview:self.nameLogoL];
    [self.contentView addSubview:self.idlogoL];
    [self.contentView addSubview:self.nameL];
    [self.contentView addSubview:self.idL];
    [self.contentView addSubview:self.arrowButton];
    [self.indexL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(relativeWidth(50));
    }];
    [self.nameLogoL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(relativeWidth(5));
        make.left.mas_equalTo(self.indexL.mas_right);
    }];
    [self.idlogoL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(relativeWidth(-5));
        make.left.mas_equalTo(self.nameLogoL);
    }];
    [self.arrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(relativeWidth(-5));
        make.centerY.mas_equalTo(0);
        make.width.height.mas_equalTo(relativeWidth(26));
    }];
    [self.nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.arrowButton.mas_left).offset(relativeWidth(-5));
        make.width.mas_equalTo(relativeWidth(200));
        make.centerY.mas_equalTo(self.nameLogoL);
    }];
    [self.idL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.width.mas_equalTo(self.nameL);
        make.centerY.mas_equalTo(self.idlogoL);
    }];
}

- (void)setModel:(DetailListModel *)model {
    _model = model;
    self.arrowButton.selected = model.isOpen;
    self.indexL.text = [NSString stringWithFormat:@"%d",model.index+1];
    self.nameLogoL.text = @"用品名";
    self.idlogoL.text = @"标识码";
    self.nameL.text = model.name;
    self.idL.text = model.identifyId;
}

- (void)openClick:(UIButton *)button {
    button.selected = !button.selected;
    self.model.isOpen = !self.model.isOpen;
    if(self.openBlock){
        self.openBlock(self.model.index);
    }
}

- (UILabel *)indexL {
    if(!_indexL){
        UILabel *indexL = [UILabel new];
        indexL.font = [UIFont systemFontOfSize:relativeWidth(16)];
        indexL.textColor = [UIColor blackColor];
        indexL.textAlignment = NSTextAlignmentCenter;
        _indexL = indexL;
    }
    return _indexL;
}

- (UILabel *)nameLogoL {
    if(!_nameLogoL){
        UILabel *nameLogoL = [UILabel new];
        nameLogoL.font = [UIFont systemFontOfSize:relativeWidth(16)];
        nameLogoL.textColor = [UIColor blackColor];
        _nameLogoL = nameLogoL;
    }
    return _nameLogoL;
}

- (UILabel *)idlogoL {
    if(!_idlogoL){
        UILabel *idlogoL = [UILabel new];
        idlogoL.font = [UIFont systemFontOfSize:relativeWidth(16)];
        idlogoL.textColor = [UIColor blackColor];
        _idlogoL = idlogoL;
    }
    return _idlogoL;
}

- (UILabel *)nameL {
    if(!_nameL){
        UILabel *nameL = [UILabel new];
        nameL.font = [UIFont systemFontOfSize:relativeWidth(14)];
        nameL.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        nameL.textAlignment = NSTextAlignmentRight;
        _nameL = nameL;
    }
    return _nameL;
}

- (UILabel *)idL {
    if(!_idL){
        UILabel *idL = [UILabel new];
        idL.font = [UIFont systemFontOfSize:relativeWidth(14)];
        idL.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        idL.textAlignment = NSTextAlignmentRight;
        _idL = idL;
    }
    return _idL;
}

- (NewsBigButton *)arrowButton {
    if(!_arrowButton){
        NewsBigButton *arrowButton = [NewsBigButton new];
        [arrowButton setBackgroundImage:[UIImage imageNamed:@"arrow_right"] forState:UIControlStateNormal];
        [arrowButton setBackgroundImage:[UIImage imageNamed:@"arrow_bottom"] forState:UIControlStateSelected];
        [arrowButton addTarget:self action:@selector(openClick:) forControlEvents:UIControlEventTouchUpInside];
        _arrowButton= arrowButton;
    }
    return _arrowButton;
}

@end
